import 'package:app/core/rtc/base/session_type.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:get/get.dart';

import 'logic.dart';

class RtcPage extends GetView<RtcLogic> {
  final _arguments = Get.arguments;
  final RtcLogic _logic = Get.find<RtcLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(width: Get.width, height: Get.height, child: _rtcChat()),
    );

    // return GetBuilder<RtcLogic>(initState: (_) {
    // }, builder: (_logic) {
    //   return Container(width: Get.width, height: Get.height, child: _rtcChat());
    // });
  }

  // 音视频
  Widget _rtcChat() {
    return _chatView();
  }

  Widget _chatView() {
    Widget _widget;
    switch (_arguments["type"]) {
      case SessionType.AUDIO:
        _widget = Text("data");
        break;
      case SessionType.VIDEO:
        _widget = _rtcVideo(_logic);
        break;
      case SessionType.DATA:
        _widget = Text("data");
        break;
      case SessionType.SCREEN:
        _widget = _rtcVideo(_logic);
        break;
    }
    return _widget;
  }

  // 音频
  Widget _rtcVideo(RtcLogic logic) {
    return OrientationBuilder(
      builder: (BuildContext context, Orientation orientation) {
        return Stack(children: [
          GetBuilder<RtcLogic>(initState: (_) {
            if (_arguments["response"] == null) {
              _logic.rtcCall(_arguments["phone"], _arguments["type"]);
            }
          }, builder: (_) {
            return Stack(
              children: [
                Positioned(
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    //远端视频容器,大小为大视频
                    child: Container(
                      margin: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                      //整个容器宽
                      width: double.infinity,
                      //整个容器高
                      height: double.infinity,
                      //远端视频渲染
                      child: RTCVideoView(
                        logic.state.remoteRender.value,
                        mirror: true,
                      ),
                      decoration: BoxDecoration(color: Colors.black54),
                    )),
                Positioned(
                  right: 20.0,
                  top: 20.0,
                  //本地视频容器,大小为小视频
                  child: Container(
                    //固定宽度,竖屏时为90,横屏时为120
                    width: orientation == Orientation.portrait ? 450.w : 600.w,
                    //固定高度,竖屏时为120,横屏时为90
                    height: orientation == Orientation.portrait ? 600.h : 450.h,
                    //本地视频渲染
                    child: RTCVideoView(
                      logic.state.localRender.value,
                      mirror: true,
                    ),
                    decoration: BoxDecoration(color: Colors.black54),
                  ),
                ),
              ],
            );
          }),
          Positioned(
              bottom: 20,
              child: SizedBox(
                width: MediaQuery.of(context).size.width,
                height: 68.h,
                child: Row(
                  children: [
                    Expanded(
                      flex: 1,
                      child: FloatingActionButton(
                          child: Obx(() => Icon(_logic.state.isMuteMic.value
                              ? Icons.mic_off
                              : Icons.mic)),
                          onPressed: () {
                            _logic.muteMicroPhone();
                          }),
                    ),
                    Expanded(
                        flex: 1,
                        child: FloatingActionButton(
                            child: Icon(Icons.call_end),
                            onPressed: () {
                              _logic.endCall();
                            })),
                    Expanded(
                        flex: 1,
                        child: FloatingActionButton(
                            child: Icon(Icons.switch_camera),
                            onPressed: () {
                              _logic.switchCamera();
                            })),
                  ],
                ),
              ))
        ]);
      },
    );
  }

// 视频
  Widget _rtcAudio() {}
}
